#include "ChatClient.hpp"
#include "ChatWindows.hpp"
void menu()
{
    cout << "***********************************" << endl;
    cout << "*******1.Register****2.Login*******" << endl;
    cout << "*******3.Loginout****4.Exit *******" << endl;
    cout << "***********************************" << endl;
}


int main(int argc,char* argv[])
{

    //./ChatClient -ip
    if(argc != 3)
    {
        LOG(INFO, "please using ./ChatClient -ip") << endl;
        return -1;
    }
    string ip;
    for(int i = 0; i < argc;i++)
    {
        if(strcmp("-ip",argv[i]) == 0 && i+1 < argc)
        {
            ip = argv[i+1];
        }
    }
    if(ip.size() == 0)
    {
        LOG(ERROR,"Illegal IP") << endl;
        return -1;
    }
    else if(ip != "159.75.33.10")
    {
        LOG(ERROR,"Illegal IP") <<endl;
        return -1;
    }
    ChatClient* cc = new ChatClient(ip);
    ChatWindows* cw = new ChatWindows();
    while(1)
    {
        menu();
        cout << "please enter your select:";
        fflush(stdout);
        int select = -1;
        cin >> select;
        switch(select)
        {
            case REGISTER:
                {
                    int ret = cc->RegistertoSvr();
                    if(ret < 0)
                    {
                        LOG(ERROR,"Register faild! please retry register...") << endl;
                    }
                    else 
                    {
                        LOG(INFO,"Register success! please login...") << endl;
                    }
                    cc->Closefd();
                    break;
                }
            case LOGIN:
                {
                    int ret = cc->LogintoSvr();
                    if(ret < 0)
                    {
                        LOG(ERROR,"Login faild! please retry login...") << endl;
                    }
                    else 
                    {
                        LOG(INFO,"Login success! please chat...") << endl;
                    }
                    while(1)
                    {
                        cw->Start(cc);     
                    }
                    cc->Closefd();
                    break;
                }
            case LOGINOUT:
                {

                }
            case EXIT:
                {
                    LOG(INFO,"exit success...") << endl;
                    exit(0);
                    break;
                }

        }
    }
    return 0;
}
